class Solution {
public:
    int countLargestGroup(int n) {
        unordered_map<int, int> hashMap;
        int maxValue = 0;
        for (int i = 1; i <= n; i++) {
            int key = 0, x = i;
            while (x) {
                key += x % 10;
                x /= 10;
            }
            ++hashMap[key];
            maxValue = max(maxValue, hashMap[key]);
        }
        int count = 0;
        for (auto &kpair : hashMap) {
            if (kpair.second == maxValue) {
                ++count;
            }
        }
        return count;
    }
};
